{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <a id='toc1_'></a>[Stimulus-coding, response-coding, or accuracy coding? A complete guide](#toc0_)\n",
    "\n",
    "- Author:: [Wanke Pan](https://github.com/panwanke)\n",
    "- Email:: [panwanke2023@gmail.com](mailto:panwanke2023@gmail.com)\n",
    "- Tags:: [python, hddm, data-analysis, stimulus-coding, response-coding, accuracy-coding]\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "Note:\n",
    "\n",
    "The practical aspects of model fitting using different coding schemes in HDDM will be covered in the [second part of this guide]()."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Table of contents**<a id='toc0_'></a>    \n",
    "- [Stimulus-coding, response-coding, or accuracy coding? A complete guide](#toc1_)    \n",
    "    - [Introduction](#toc1_1_1_)    \n",
    "    - [Basic introduction for DDM](#toc1_1_2_)    \n",
    "    - [DDM likelihood and generation model](#toc1_1_3_)    \n",
    "  - [Three Coding Pattern in DDM](#toc1_2_)    \n",
    "    - [Accuracy coding vs. Choice coding](#toc1_2_1_)    \n",
    "      - [Perceptual decision making and Accuracy coding](#toc1_2_1_1_)    \n",
    "      - [Value Decision and Choice Coding](#toc1_2_1_2_)    \n",
    "  - [Impact of coding on parameter interpretation](#toc1_3_)    \n",
    "    - [The interpretation of Drift rate (v)](#toc1_3_1_)    \n",
    "    - [The interpretation of Decision boundary (a)](#toc1_3_2_)    \n",
    "    - [The interpretation of Start point (z)](#toc1_3_3_)    \n",
    "    - [Summary](#toc1_3_4_)    \n",
    "  - [Mixed coding, stimulus coding](#toc1_4_)    \n",
    "  - [Formula with different codings](#toc1_5_)    \n",
    "    - [Stimulus coding](#toc1_5_1_)    \n",
    "    - [Stimulus Coding Type2](#toc1_5_2_)    \n",
    "\n",
    "<!-- vscode-jupyter-toc-config\n",
    "\tnumbering=false\n",
    "\tanchor=true\n",
    "\tflat=false\n",
    "\tminLevel=1\n",
    "\tmaxLevel=6\n",
    "\t/vscode-jupyter-toc-config -->\n",
    "<!-- THIS CELL WILL BE REPLACED ON TOC UPDATE. DO NOT WRITE YOUR TEXT IN THIS CELL -->"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <a id='toc1_1_1_'></a>[Introduction](#toc0_)\n",
    "\n",
    "The Drift Diffusion Model (DDM) has become very popular in psychology, decision making, and neuroscience, and can represent how individuals accumulate evidence when making decisions (see [Basic introduction for DDM](#toc1_1_2_)). \n",
    "\n",
    "However, a fundamental question is being ignored? What exactly is the information or evidence being accumulated? Where does the decision bias go? What is the purpose of decision caution? \n",
    "\n",
    "It all relates to the endpoint of the decision-outcomes coding-i.e., whether the individual chose option A or option B in a value decision; whether the individual responded correctly or incorrectly in a perceptual decision. \n",
    "\n",
    "While you are interested in choice coding in DDM, you must also be puzzled by such questions:\n",
    "\n",
    "- In value-based decision making, decision bias can respond to an individual's preference for option A or B. So in perceptual decision making, does decision bias respond to a bias for correct or incorrect responses? \n",
    "- Even though you may know that in perceptual decision making, response bias represents a bias towards correct or incorrect responses, you may be more interested in learning more about what causes this response bias? And why response bias in DDM has different interpretations in different studies.\n",
    "\n",
    "Clearly, there are some differences in the research questions, theoretical assumptions, model settings, and parameter assumptions behind the coding of Response (Option A and Option B) vs. Accuracy (Correct and Error). Here today, I attempt to clear these confusions.\n",
    "\n",
    "- Firstly, I will introduce the typical research paradigms as well as data structures for different coding patterns.\n",
    "- Afterwards, The implications of different coding styles for modeling and parameter interpretation were discussed.\n",
    "- Finally, I will show how to implement the different coding models in HDDM (Wiecki et al., 2013)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Who this post is for**\n",
    "\n",
    "Here’s what I assume you know:\n",
    "- **Readers are familiar with the basic principles of the Diffusion Decision Model (DDM).** This includes an understanding of the drift rate, starting point, and boundary separation parameters, as well as the relationship between these parameters and response accuracy and reaction time.\n",
    "- **Readers can perform basic model fitting using HDDM.** This includes being able to load data into HDDM, specify the model parameters, and run the model.\n",
    "- **Readers have heard of perceptual decision-making and value-based decision-making.** Perceptual decision-making refers to decisions made based on sensory information, while value-based decision-making refers to decisions made based on the subjective value of different options.\n",
    "- **Readers are aware that the decision variable in the DDM can be either accuracy or choice.** In the accuracy task, participants are asked to respond whether a stimulus is present or absent. In the choice task, participants are asked to choose between two or more options."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <a id='toc1_1_2_'></a>[Basic introduction for DDM](#toc0_)\n",
    "\n",
    "If you already have a better understanding of the underlying assumptions and parameter meanings of DDM, you can easily skip this section.\n",
    "\n",
    "![DDM_illus1.png](../figs/fig1_DDM_illustration.png)\n",
    "\n",
    "\n",
    "|parameters|meanings|\n",
    "|---|---|\n",
    "|Drift rate ($v$) |Rate of evidence accumulation processing|\n",
    "|Decision Threshold ($a$) |Response caution|\n",
    "|Non-decision time ($t$) |Time of perception, movement initiation and execution|\n",
    "|Decision-making bias ($z$) |Starting point relative to the two boundaries|\n",
    "\n",
    "\n",
    "The DDM model, known as the Drift-Diffusion Model, is a process cognitive model for binary decision-making tasks. It can help us understand how individuals accumulate evidence and make choices during decision making. \n",
    "\n",
    "The model is based on several key assumptions: the process of evidence accumulation, decision thresholds, drift rates, and starting points.\n",
    "\n",
    "1. The evidence accumulation process hypothesizes that individuals accumulate evidence continuously from experimental stimuli during the decision-making process, depending on the purpose of the task. For example, in a random point motion task (see ???), an individual can observe the process of identifying which points are moving randomly and which points are moving in a fixed direction, and this process of identifying or perceiving the moving points is the accumulation of information or evidence about the direction of the point motion.\n",
    "\n",
    "2.  When the amount of accumulated evidence reaches or exceeds the decision threshold, individuals make decisions accordingly. Therefore, when the decision boundary is higher, it is assumed that the individual spends more time to obtain more evidence; conversely, a smaller decision boundary means that the individual spends less time and obtains less evidence, representing that their decisions are not so cautious, i.e., more risky.\n",
    "\n",
    "3.  The rate at which information accumulates can be expressed in terms of the drift rate v. It is conceivable that the greater the drift rate, the faster the individual acquires information and the easier it is to reach the decision boundary. \n",
    "\n",
    "4.  The starting point reflects the decision bias. When the starting point is approximately close to the upper boundary, then the more likely it is to produce a decision response corresponding to the upper boundary.\n",
    "\n",
    "We do not provide here a full explanation of the foundations of DDM, such as parametric non-decision time. To avoid going too far off topic, we recommend that interested readers read (Ratcliff et al., 2016).\n",
    "\n",
    "> Ratcliff et al. (2016) 'Diffusion Decision Model: Current Issues and History', Trends in Cognitive Sciences, 20(4), 260-281."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <a id='toc1_1_3_'></a>[DDM likelihood and generation model](#toc0_)\n",
    "\n",
    "The expression for the DDM is as follows, with the behavioral decision outcome, reaction time and response on the left; and the Wiener first-passage times distribution on the right, containing the four parameters in the DDM:\n",
    "\n",
    "$$\n",
    "rt,choice\\sim \\text{WFPT}(v,a,t,z)\n",
    "$$\n",
    "\n",
    "This expression implies that the four parameters of the DDM can be estimated when we know the behavioral decision outcomes (reaction times and choice). \n",
    "\n",
    "In turn, when we know the four parameters of the DDM, we can simulate reaction times and responses.\n",
    "\n",
    "- It should be noted that we assume that z is a real number from 0 to 1. When z = 0.5, it indicates that the starting point is located at the focus a/2 of the decision boundary range a, indicating that there is no decision bias. When z > 0.5 indicates that there is a bias towards the upper boundary; when z < 0.5 indicates that there is a bias towards the lower boundary.\n",
    "- For specific mathematical derivations we recommend reading (Navarro & Fuss, 2009)\n",
    "\n",
    "> Navarro and Fuss (2009) 'Fast and accurate calculations for first-passage times in Wiener diffusion models', *Journal of Mathematical Psychology*, 53(4), 222-230."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <a id='toc1_2_1_'></a>[Accuracy coding vs. Choice coding](#toc0_)\n",
    "\n",
    "### <a id='toc1_2_1_1_'></a>[Perceptual decision making and Accuracy coding](#toc0_)\n",
    "\n",
    "In the most classical DDM, it is usually based on accuracy coding. We use the most ubiquitous dot motion task (Brosnan et al., 2020) to illustrate the data structure, as well as the interpretation of the model parameters.\n",
    "\n",
    "![image.png](../figs/fig2_dot_motion_task.png)\n",
    "\n",
    "When performing DDM model analyses, our data structure tends to be a long data structure, i.e., each row represents a response on one trial. \n",
    "\n",
    "|subject_idx|trial_number|stimulus|coherence|choice|accuracy|rt|\n",
    "|-|-|-|-|-|-|-|\n",
    "|1|1|left|20%|right|0|0.56|\n",
    "|1|2|left|50%|left|1|0.48|\n",
    "|1|3|right|50%|right|1|0.52|\n",
    "|1|4|left|20%|right|0|0.61|\n",
    "|...|...|...|...|...|...|...|\n",
    "|9|79|right|20%|right|1|0.55|\n",
    "|9|80|left|50%|left|1|0.49|\n",
    "\n",
    "- Since each subject contains responses from multiple trials, we label the different subjects by `subject_idx`. \n",
    "- In addition, we represent the trial number by `trial_number`, in this example, each subject has a total of 80 trial responses. \n",
    "- `stimulus` and `coherence` are the experimental conditions, which indicate whether the motion of the random dots is to the left or to the right of the left-left or right-right; as well as the proportion of non-randomly-moving dots, e.g., 20% and 50% corresponds to the hard and the easy conditions. \n",
    "- Finally, we recorded the subject's response `choice` (whether the overall motion of the dot was to the left or to the right) and the response time `rt`. The `accuracy` of the subject's judgment can be calculated from the subject's response and the true direction of the dot's motion, with 1 being the correct response and 0 being the error response.\n",
    "\n",
    "It should be stressed that for DDM modeling, response time and choice (accuracy or response) are necessary. For hierarchical modeling, subject_id is additionally necessary.\n",
    "\n",
    "It is important to note that in this paradigm, our research question is more concerned with the individual's accuracy, i.e., the effect of the direction and consistency of the dot motion on the rate of correct response, rather than on the effect of the response being left or right. As a result, the research of perceptual decisions represented by this paradigm tends to use **Accuracy coding**.\n",
    "\n",
    "\n",
    "\n",
    "> Brosnan, M. B., Sabaroedin, K., Silk, T., Genc, S., Newman, D. P., Loughnane, G. M., Fornito, A., O’Connell, R. G., & Bellgrove, M. A. (2020). Evidence accumulation during perceptual decisions in humans varies as a function of dorsal frontoparietal organization. Nature Human Behaviour, 4(8), Article 8. https://doi.org/10.1038/s41562-020-0863-4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <a id='toc1_2_1_2_'></a>[Value Decision and Choice Coding](#toc0_)\n",
    "\n",
    "For value-based decision making, let's take the task of food choice as an example. Individuals will be confronted with two different snacks, such as chocolate, crisps, biscuits, etc. These snacks will usually have some specific attributes such as taste, flavor, healthiness, etc. so that the subject can make a choice based on these attributes. One of the options is labeled option A and the other is labeled as option B.\n",
    "\n",
    "![image.png](../figs/fig3_food_choice_task.png)\n",
    "\n",
    "Options A and B are not fixed and can change location in each trial. In addition, subjects subjectively reported preference values (`option_A_value` and `option_B_value`) for different snacks in advance. Therefore, we can calculate the consistency (`accuracy`) of subjects' choice behavior and subjective preferences.\n",
    "\n",
    "|subject_idx|trial_number|option_A_value|option_B_value|loc_optionA|choice|accuracy|rt|\n",
    "|---|---|---|---|---|---|---|---|\n",
    "|1|1|6|5|left|A|0|0.56|\n",
    "|1|2|8|4|right|A|1|0.48|\n",
    "|1|3|5|7|left|B|1|0.52|\n",
    "|1|4|8|7|right|B|0|0.61|\n",
    "|...|...|...|...|...|...|...|...|\n",
    "|9|79|6|8|left|B|1|0.55|\n",
    "|9|80|7|3|right|A|1|0.49|\n",
    "\n",
    "It is worth noting that in value-based decision making, we focus more on which snack an individual chooses, i.e., option A or B, rather than whether the choice is correct or incorrect. An interesting study is that individuals prefer a snack when they pay more attention to it, suggesting that an individual's preference for a particular snack is not fixed and is susceptible to cognitive factors such as attributes of the snack and attention (Krajbich et al., 2010). \n",
    "\n",
    "Conceivably, while consistency in behavioral and psychological preferences is also important, researchers care more about the effects of snack attributes and attention on the frequency and pattern of snack selection. For this reason, **Choice Coding** is often used in this research.\n",
    "\n",
    "Another more stark example is that in risky decision making, option A and option B may correspond to risky and conservative decisions, and the individual's preference for risk changes over time and is also influenced by the value of the option. Thus, we cannot use accuracy coding.\n",
    "\n",
    "> Krajbich, I., Armel, C., & Rangel, A. (2010). Visual fixations and the computation and comparison of value in simple choice. Nature Neuroscience, 13(10), 1292–1298. https://doi.org/10.1038/nn.2635"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <a id='toc1_3_'></a>[Impact of coding on parameter interpretation](#toc0_)\n",
    "\n",
    "\n",
    "|  | Keys | Response | Accuracy | \n",
    "| ---- | ---- | ---- | ---- |\n",
    "| coding example | 1 = left  <br>0 = right | 1 = option A  <br>0 = option B | 1 = correct  <br>0 = error | \n",
    "| typical paradigm |  | food decision | dot motion task |  |\n",
    "| interpretation of drift rate (v) | Bias to left | Preference or fast information uptaking for option A | Fast accumulating valid evidence for discrimination |  |\n",
    "| interpretation of start point (z) | Bias to left | Bias to option A | Pre-judgment evidence | \n",
    "| interpretation of boundary (a) | Boundary separation between left key and right key | Boundary separation between option A and option B | Boundary separation between correct and incorrect response | \n",
    "| Features | The left may correspond to \"match\", \"shoot\", or \"accept\" | Differ from keys coding, while the option A could occur at left or right | Only in one of two experiment conditions, the \"left\" is correct or optimal. |\n",
    "\n",
    "\n",
    "\n",
    "We summarize the three encodings schema in the table. \n",
    "- In the key encoding, all three parameters are interpreted relative to the key, and reflect a bias towards responding to a particular key (e.g. left). Note that keys can be assigned different meanings, e.g. left may correspond to \"match\", \"shoot\", or \"accept \".\n",
    "- **choice coding** can be seen as an extension of keystroke coding, e.g., the left button for option A and the right button for option B. The difference is that the mapping of options to keys can vary across trials, i.e., option A may also appear on the left at the t-th trial but on the right at the t+1 trial. At this point the interpretation of the parameters is relative to the options.\n",
    "- **Accuracy coding** differs from the first two coding patterns in that individuals may obtain correct or incorrect results for both keys/options. At this point, the interpretation of the parameter changes more dramatically. Next, we explain the different coding modes and their effects on parameter interpretation:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <a id='toc1_3_1_'></a>[The interpretation of Drift rate (v)](#toc0_)\n",
    "\n",
    "Under the accuracy coding scheme of perceptual decision-making, the drift rate (v) represents the individual's ability to discriminate between the signal and noise in the stimulus. It is conceivable that individuals are better able to accumulate evidence of the exact direction of motion of the dot in a simple task (coherence=50%) relative to a more difficult task (coherence=20%), i.e., \"v\" is large.\n",
    "\n",
    "With choice coding in value decision making, a larger drift rate would only indicate a faster information processing for a particular option (e.g., snack). \n",
    "\n",
    "Moreover, across trials, individuals may sometimes prefer option A and sometimes option B, which would result in different signs of \"v\" in different stimulus conditions (A positive drift rate (v) indicates a preference for option A, while a negative drift rate indicates a preference for option B). \n",
    "\n",
    "Under accuracy coding, the interpretation of different signs of \"v\" remains largely the same. A negative drift rate (v) still indicates that the individual is more likely to perceive the signal (which is associated with option A) and less likely to perceive the noise (which is associated with option B). Conversely, a positive drift rate indicates that the individual is more likely to perceive the noise and less likely to perceive the signal."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <a id='toc1_3_2_'></a>[The interpretation of Decision boundary (a)](#toc0_)\n",
    "\n",
    "An important reason for adopting Accuracy coding is to explain the **speed-accuracy tradeoff**, which refers to the fact that individuals need to slow down if they want to respond more correctly. \n",
    "\n",
    "The decision boundary (a) corresponds to the interpretation of the speed-accuracy trade-off, where a lower value of \"a\" indicates a lower decision threshold, and individuals are more likely to make decisions with less accumulated evidence, thus speeding up decision making. However, lower thresholds may lead to higher error rates, as individuals are more likely to be influenced by random noise and make poor decisions. Conversely, a higher \"a\" value indicates a higher decision-making threshold, where individuals need to accumulate more evidence to make a decision, which reduces the error rate, but may take more time.\n",
    "\n",
    "However, for the use of choice coding in value-based decision making, the interpretation of the decision boundary (a) changes, and might be described as a speed-preference trade-off, or a speed-satisfaction trade-off. On the one hand, higher values of \"a\" may indicate that individuals spend more time forming a preference for a particular snack rather than accumulating evidence about differences in preferences for two snacks. On the other hand, a low \"a\" value may simply indicate that an individual needs only a small amount of preference information to make a satisfactory decision. This is often the case in situations of time pressure. It's easy to imagine that if you have enough time to shop, you will choose the optimal item that satisfies you the most, but if there is a sale down the street, you might buy an item you don't need in a moment of desperation."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <a id='toc1_3_3_'></a>[The interpretation of Start point (z)](#toc0_)\n",
    "\n",
    "Under the use of choice coding in value decision making, the start point represents a preference for an option. The \"z\" > 0.5 means that the individual has a pre-stimulus preference for option A. Therefore, A is more likely to be chosen and chosen quickly. It is important to note that, in contrast to the drift rate, which affects preference during the process of decision making, the start point affects preference prior to the process, which means that even though option B has a high endowment of preference, an individual cannot form a preference for option B without accumulating information for option B.\n",
    "\n",
    "However, under Accuracy coding, the interpretation of the start point becomes ambiguous, which contrasts with the interpretation of the decision boundary, where Accuracy coding provides a clearer interpretation of the decision boundary and choice coding provides a more ambiguous interpretation of it. Intuitively, higher \"z\" indicate that individuals own evidence for the correct response before they observe the stimulus. However, the plausibility of this interpretation must be combined with a particular experimental design. For example, in the priming paradigm, priming cues provide evidence for stimulus before it is observed. \n",
    "\n",
    "Alternatively, the experiment manipulated two conditions. In one condition, individuals observed stimuli that were more familiar, while in the other condition they observed unfamiliar stimuli. The a priori information from this familiarity impacts the \"z\". However, based on previous research, familiarity, or priming, not only affects the onset point, but may also affect the drift rate (Ratcliff et al., 2004), which makes it difficult to distinguish between the effects of experimental manipulations on specific cognitive processes. \n",
    "\n",
    "Therefore, a more common manipulation in Accuracy coding models is to fix the \"z=0.5\", i.e., to assume that there is no decision bias, which also eliminates the necessity for interpretation of this parameter.\n",
    "\n",
    "> Ratcliff, R., Gomez, P., & McKoon, G. (2004). A Diffusion Model Account of the Lexical Decision Task. Psychological Review, 111(1), 159–182. https://doi.org/10.1037/0033-295X.111.1.159"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <a id='toc1_3_4_'></a>[Summary](#toc0_)\n",
    "\n",
    "Finally, it should be emphasized that the choice coding and parameter interpretation is not unchanged, it is related to the experimental design and model setting. Here, only two of the most typical coding patterns are illustrated. \n",
    "\n",
    "Of course, the reader never wants to stop there, and we have not responded to the most crucial question: how to simultaneously satisfy the assumptions \n",
    "- a) letting the drift rate and decision boundary respond to the performance ability and  the speed-accuracy trade-off; \n",
    "- and b) letting the start point respond to the decision bias for a particular option or keystroke? \n",
    "\n",
    "😎 Next we will formally introduce the mixed coding model and stimulus coding."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <a id='toc1_4_'></a>[Mixed coding, stimulus coding](#toc0_)\n",
    "\n",
    "It has to be noted that we use the terms *Choice coding* and *Accuracy coding*, which may be different from the terminology you may see named *Stimulus coding*. This is because we want to make a distinction between these concepts. The main reason for this is the pioneering use of *\"stimulus coding\"* in tools represented by the HDDM model, without a clear distinction between this concept and *\"choice coding\"* or \"\"response coding\"\".\n",
    "\n",
    "> We believe that *Response* is a superordinate concept to which *Accuracy* and *Choice* belong. And *Stimulus coding* is actually short name for *response to stimulus*. \n",
    "\n",
    "As mentioned earlier, we are interested in testing two specific hypotheses: (1) whether the decision boundary (a) responds to the speed-accuracy trade-off, and (2) whether the starting point (z) responds to the decision bias towards a particular stimulus or option. As it turns out, **neither the choice coding nor the accuracy coding scheme can address the problem of combining these two hypotheses. However, the Stimulus coding proposed in HDDM solves this problem.**\n",
    "\n",
    "||Response|Accuracy|Stimulus|Stimulus Type2|\n",
    "|---|---|---|---|---|\n",
    "|coding for drift rate (v)|1 = option A  <br>0 = option B|1 = correct  <br>0 = error|1 = correct  <br>0 = error|1 = left  <br>0 = right|\n",
    "|interpretation of v|Preference to option A|Valid evidence for discrimination|Valid evidence for discrimination|Preference to keys|\n",
    "|coding for boundary (a)|1 = option A  <br>0 = option B|1 = correct  <br>0 = error|1 = correct  <br>0 = error|1 = option A  <br>0 = option B|\n",
    "|interpretation of a|Caution for option A|Caution for correct|Caution for correct|Caution for option A|\n",
    "|coding for start point (z)|1 = option A  <br>0 = option B|1 = correct  <br>0 = error|1 = option A  <br>0 = option B|1 = option A  <br>0 = option B|\n",
    "|interpretation of z|Bias to option A|Pre-judgment evidence|Bias to option A|Bias to option A|\n",
    "\n",
    "\n",
    "The most commonly used Stimulus coding is in the fourth column of the table. It is similar to accuracy coding, the only difference being the assumption that the start point is affected by the stimulus or option (in red). As such, its interpretation of the parameters is also similar to accuracy coding, with the drift rate representing the rate at which the cumulative evidence makes a correct response; the decision boundary represents the speed-accuracy trade-off, i.e., spending more time is more likely to result in a correct response. At the same time, its start point responds to a bias toward keystrokes or options.\n",
    "\n",
    "Along these lines, we can consider a second type of stimulus coding, corresponding to column 5 in the table. As compared to choice coding, this Stimulus coding assumes that cumulative preference is associated with key presses rather than the options themselves. An example of this is that for right-handed people, individuals may press the right button more often in an uncertainty decision task, and this response bias may ultimately lead to a wrong interpretation of the response preference for option B. Therefore, this coding model is suitable for applying the keypress bias present in the choice coding model."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <a id='toc1_5_'></a>[Formula with different codings](#toc0_)\n",
    "\n",
    "For both choice coding and accuracy coding, we could use the basic DDM expression to modeling (see [DDM likelihood and generation model](#toc1_1_3_)):\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "rt,choice=1&\\sim \\text{WFPT}(v,a,t,z) \\\\\n",
    "rt,choice=0&\\sim \\text{WFPT}(-v,a,t,1-z) \\\\\n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "This expression shows that\n",
    "- When the decision is Option A or Correct Response (choice = 1), the direction of information accumulation is upward, i.e., drift rate \"v\" is positive. At this point the start point \"z\" expresses that the information prior to the decision is relative to option A or correct response.\n",
    "- When the decision is option B or the response is incorrect (choice=0), \"v\" is negative and the direction of information accumulation is downward. At this point, the start point is \"1-z\", which expresses that the information before the decision is relative to option B or the wrong response.\n",
    "\n",
    "Based on this expression, we can understand the symmetry assumption of the classical DDM:\n",
    "- If the evidence accumulation rate for option A and correct response is \"v\", then the rate for option B and incorrect response is also \"v\", except that the sign of the \"v\" is positive or negative. That is, it is symmetric with respect to point 0.\n",
    "- If the decision bias is \"z\" for choice A and when the response is correct, then the \"1-z\" for choice B and response error, i.e., are symmetric with respect to the center of 0.5.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <a id='toc1_5_1_'></a>[Stimulus coding](#toc0_)\n",
    "\n",
    "\\begin{align*}\n",
    "rt,(choice=1|stim=1) &\\sim \\text{WFPT}(v,a,t,z) \\\\\n",
    "rt,(choice=0|stim=1) &\\sim \\text{WFPT}(-v,a,t,1-z) \\\\\n",
    "rt,(choice=1|stim=0) &\\sim \\text{WFPT}(v,a,t,1-z) \\\\\n",
    "rt,(choice=0|stim=0) &\\sim \\text{WFPT}(-v,a,t,z) \n",
    "\\end{align*}\n",
    "\n",
    "When using stimulus coding, we need to consider additional variables stim, such as dot_motion in the previous example data.\n",
    "- When stim=1 (i.e., `stimulus=\"left\"`), the DDM expression is consistent with the previous expression.\n",
    "- When stim=0 (i.e., `stimulus=\"right\"`), the z parameter in the DDM expression is transformed, while the rest of the participation remains unchanged.\n",
    "\n",
    "Note that at this point, our choice stands for correctness, i.e., choice=1 equals accuracy=1, the correct response.\n",
    "- Thus, the first line of the formula expresses that when the stimulus is moved to the left, the response is correct, the response is left, and the drift rate and start point point to the response being left (assuming v>0,z>0.5).\n",
    "- The second line of the formula expresses that when the stimulus is moved to the left, the response is incorrect, the response is right, and the drift rate and start point point to the response being right.\n",
    "- The third line of the formula expresses that when the stimulus is moved to the right, the response is correct, **the response is right, the drift rate points to the response being left, and the start point points to the response being right**.\n",
    "- The fourth line of the formula expresses that when the stimulus moves to the right, the response is incorrect, **the response is left, the drift rate points to the response as right, and the start point points to the response as left**.\n",
    "\n",
    "The third and fourth rows responded that the start point bias pointed in the direction of the response regardless of the direction of stimulus movement. Thus, with this mixed coding setting, we can account for the decision bias for the direction of the response.\n",
    "\n",
    "It is important to note that although we refer to this mixed coding approach as stimulus coding, the dependent variable we actually use is accuracy. It is called stimulus coding because only the start point is affected by the stimulus.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <a id='toc1_5_2_'></a>[Stimulus Coding Type2](#toc0_)\n",
    "\n",
    "$$\n",
    "\\begin{align*}\n",
    "rt,(choice=1|stim=1) &\\sim \\text{WFPT}(v,a,t,z) \\\\\n",
    "rt,(choice=0|stim=1) &\\sim \\text{WFPT}(-v,a,t,1-z) \\\\\n",
    "rt,(choice=1|stim=0) &\\sim \\text{WFPT}(-v,a,t,z) \\\\\n",
    "rt,(choice=0|stim=0) &\\sim \\text{WFPT}(v,a,t,1-z) \n",
    "\\end{align*}\n",
    "$$\n",
    "\n",
    "In the example of value decision and choice coding, we consider the location `loc_optionA`:\n",
    "- When stim=1 (i.e., `loc_optionA=\"left\"`), the DDM expression is consistent with the first previous expression.\n",
    "- When stim=0 (i.e., `loc_optionA=\"right\"`), the \"v\" parameter in the DDM expression is transformed, while the rest of the participation remains unchanged.\n",
    "\n",
    "Note that at this point our choice variable is response, i.e., choice=1 equals to response=1, i.e., choose option A.\n",
    "- Thus, the first line of the formula expresses when option A is on the left,  choosing option A, i.e., choosing the left option, the drift rate and start point pointing to the left (assuming v>0,z>0.5).\n",
    "- The second line of the formula expresses that when option A is on the left, selecting option B, i.e., **selecting the right option, the drift rate and start point point to the right**.\n",
    "- The third line of the formula expresses that when option A is on the right, selecting option A, i.e., **selecting the right option, the drift rate points to the right and the start point points to the left**.\n",
    "- The fourth line of the formula expresses that when option A is on the right, selecting option B, i.e., selects the left option, the drift rate points to the left and the start point points to the right.\n",
    "\n",
    "Similarly, the third and fourth rows respond that the direction of evidence accumulating process is not a preference for option A, but a bias for the positions of the options, regardless of whether option A is on the left or the right. Thus, with this mixed coding setting, we can account for the decision bias for response direction.\n",
    "\n",
    "It is also important to note that the actual dependent variable used for this stimulus coding was the response to the option, other than accuracy."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Summary\n",
    "\n",
    "In this comprehensive guide, we have explored the three main coding schemes used in the Diffusion Decision Model (DDM): accuracy coding, choice coding, and stimulus coding. We have discussed the advantages and disadvantages of each coding scheme, and we have provided detailed explanations of how to interpret the drift rate (v), decision boundary (a), and starting point (z) parameters under each coding scheme.\n",
    "\n",
    "Accuracy coding is the most straightforward coding scheme, and it is often used in perceptual decision-making tasks where participants are asked to indicate the presence or absence of a stimulus.\n",
    "\n",
    "choice coding is a more complex coding scheme that is often used in choice tasks where participants are asked to choose between two or more options.\n",
    "Stimulus coding is a relatively new coding scheme that can be used to address a wider range of research questions than accuracy coding or choice coding.\n",
    "\n",
    "Note:\n",
    "\n",
    "The practical aspects of model fitting using different coding schemes in HDDM will be covered in the [second part of this guide]().\n",
    "\n",
    "Overall, this guide provides a comprehensive overview of the different coding schemes used in the DDM. It is an essential resource for researchers who are interested in using the DDM to investigate decision-making."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py39hddm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
